package com.lhdx.practical.utils;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.List;

public class GsonUtils {
    public static String toJsonWithGson(Object obj) {
        Gson gson = createGson();   //new Gson();
        return gson.toJson(obj);
    }

    public static String toJsonWithGson(Object obj, Type type) {
        Gson gson = createGson();   //new Gson();
        return gson.toJson(obj, type);
    }

    @SuppressWarnings("unchecked")
    public static String toJsonWithGson(List list) {
        Gson gson = createGson();   //new Gson();
        return gson.toJson(list);
    }

    @SuppressWarnings("unchecked")
    public static String toJsonWithGson(List list, Type type) {
        Gson gson = createGson();   //new Gson();
        return gson.toJson(list, type);
    }

    public static String toJsonWithGsonBuilder(Object obj) {
        Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
        return gson.toJson(obj);
    }

    public static String toJsonWithGsonBuilder(Object obj, Type type) {
        Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
        return gson.toJson(obj, type);
    }

    @SuppressWarnings("unchecked")
    public static String toJsonWithGsonBuilder(List list) {
        Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
        return gson.toJson(list);
    }

    @SuppressWarnings("unchecked")
    public static String toJsonWithGsonBuilder(List list, Type type) {
        Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
        return gson.toJson(list, type);
    }

    public static <T> Object fromJson(String json, Class<T> clazz) {
        Object obj = null;
        try {
            Gson gson = new Gson();
            obj = gson.fromJson(json, clazz);
        } catch (Exception e) {
            //logger.error("fromJson方法转换json串到实体类出错", e);
        }
        return obj;
    }

    /**
     * 如果 Long 的数字超过15位，转换为String，在json中数字两边有引号
     * @return
     */
    private static Gson createGson(){
        GsonBuilder gsonBuilder = new GsonBuilder();
        LongSerializer serializer = new LongSerializer();
        gsonBuilder.registerTypeAdapter(Long.class, serializer);
        gsonBuilder.registerTypeAdapter(long.class, serializer);
        Gson gson = gsonBuilder.create();
        return gson;
    }

    public static void main(String... args) throws Exception{
//        long a = 12345678901234578L;
//
//        GsonBuilder builder = new GsonBuilder();
//        builder.registerTypeAdapter(Long.class, new LongSerializer());
//        Gson gson2 = builder.create();
//        System.out.println(gson2.toJson(a));
//
//        Gson gson = new GsonBuilder().setExclusionStrategies(new MyExclusionStrategy()).serializeNulls().create();
//        String str = gson.toJson(a);
//        System.out.println(str);

        TestVO vo = new TestVO();
        vo.setId(618708732263538688L);
        vo.setId2(918708732263538688L);
        System.out.println(toJsonWithGson(vo));

    }

    static class LongSerializer implements JsonSerializer<Long> {
        public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
            if(src!=null){
                String strSrc = src.toString();
                if(strSrc.length()>15){
                    return new JsonPrimitive(strSrc);
                }
            }
            return new JsonPrimitive(src);
        }
    }

    static class TestVO {
        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

        private long id;

        public Long getId2() {
            return id2;
        }

        public void setId2(Long id2) {
            this.id2 = id2;
        }

        private Long id2;
    }
}
